//
//  WKPlanBookCollectCell.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/17.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKPlanBookCollectCell.h"
#import "WKCommentView.h"
#import "WKContenCellRewardView.h"


@interface WKPlanBookCollectCell ()
{
    UIImageView *_headerImageview;
    UILabel *_nameLb;
    UILabel *_timeLb;
    UILabel *_contentLb;
    UIButton *_guanzhuBtn;
    
    UIImageView *_beforImgView;
    UIImageView *_afterImgView;
    UILabel *_beforLb;
    UILabel *_afterLb;
    
    WKCommentView *_commentView;
    WKContenCellRewardView *_rewardView;
}
@end

@implementation WKPlanBookCollectCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self layoutviews];
    }
    return self;
}

- (void)layoutviews{
    _headerImageview = [[UIImageView alloc]init];
    
    _nameLb = [UILabel new];
    _nameLb.font = PFSC_MediumFont(15);
    _nameLb.textColor = UIColorFromHex(0x3c3c3c);
    
    
    _timeLb = [UILabel new];
    _timeLb = [UILabel new];
    _timeLb.font = PFSC_MediumFont(11);
    _timeLb.textColor = UIColorFromHex(0x979797);
    
    
    _guanzhuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_guanzhuBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [_guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xF54050) forState:UIControlStateNormal];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateSelected];
    _guanzhuBtn.titleLabel.font = PFSC_RegularFont(11);
    _guanzhuBtn.layer.masksToBounds = YES;
    _guanzhuBtn.layer.cornerRadius = 5;
    _guanzhuBtn.layer.borderWidth = 1;
    _guanzhuBtn.layer.borderColor = UIColorFromHex(0xF54050).CGColor;
    [_guanzhuBtn addTarget:self action:@selector(guanzhuClick:) forControlEvents:UIControlEventTouchUpInside];
    _guanzhuBtn.backgroundColor = [UIColor whiteColor];
    
    
    _contentLb = [UILabel new];
    _contentLb.font = PFSC_RegularFont(15);
    _contentLb.textColor = UIColorFromHex(0x3c3c3c);
    
    
    _beforImgView = [UIImageView new];
    _afterImgView = [UIImageView new];
    _beforLb = [UILabel new];
    _beforLb.textColor = UIColorFromHex(0xFFFFFF);
    _beforLb.font = PFSC_RegularFont(10);
    _beforLb.layer.masksToBounds = YES;
    _beforLb.layer.cornerRadius = 5;
    _beforLb.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    _beforLb.text = @"Before";
    _beforLb.textAlignment = NSTextAlignmentCenter;
    
    _afterLb = [UILabel new];
    _afterLb.textColor = UIColorFromHex(0xFFFFFF);
    _afterLb.font = PFSC_RegularFont(10);
    _afterLb.layer.masksToBounds = YES;
    _afterLb.layer.cornerRadius = 5;
    _afterLb.backgroundColor = UIColorFromHex(0xF54050);
    _afterLb.text = @"Affter";
    _afterLb.alpha = 0.6;
    _afterLb.textAlignment = NSTextAlignmentCenter;
    
    _commentView = [WKCommentView new];
    _rewardView = [WKContenCellRewardView new];
    
    NSArray *views = @[_headerImageview,_nameLb,_timeLb,_contentLb,_beforImgView,_afterImgView,_commentView,_rewardView,_guanzhuBtn];
    
    
    [self.contentView wh_addSubviews:views];
    
   
    CGFloat margin = 12;
    
    [_headerImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(margin);
        make.top.mas_offset(margin);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];
    
    
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_headerImageview.mas_right).offset(margin);
        make.top.mas_equalTo(_headerImageview.mas_top);
        make.size.mas_equalTo(CGSizeMake(80, 14));
    }];
    
    
    
    
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLb.mas_left);
        make.top.mas_equalTo(_nameLb.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(200, 14));
    }];
    
    
    [_guanzhuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-margin);
        make.top.mas_equalTo(17);
        make.size.mas_equalTo(CGSizeMake(50, 24));
    }];
    
    
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.right.mas_equalTo(-margin);
        make.top.mas_equalTo(_headerImageview.mas_bottom).offset(margin);
        
        
    }];
    
    CGFloat img_width = (ScreenWidth - 28)/2;
    
    [_beforImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_contentLb.mas_bottom).offset(margin);
        make.width.mas_equalTo(img_width);
        make.height.mas_equalTo(img_width);
    }];
    
    [_beforImgView addSubview:_beforLb];
    [_beforLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.with.mas_offset(-2);
        make.bottom.with.mas_offset(2);
        make.width.mas_offset(40);
        make.height.mas_offset(20);
    }];
    
    [_afterImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_beforImgView.mas_right).offset(4);
        make.top.mas_equalTo(_beforImgView.mas_top);
        make.width.mas_equalTo(img_width);
        make.height.mas_equalTo(img_width);
    }];
    
    [_afterImgView addSubview:_afterLb];
    [_afterLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.with.mas_offset(-2);
        make.bottom.with.mas_offset(2);
        make.width.mas_offset(40);
        make.height.mas_offset(20);
    }];
   
    
    
    [_commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_beforImgView.mas_bottom).offset(margin);
        make.right.mas_equalTo(-margin);
        
    }];
    
    
    
    [_rewardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_commentView.mas_bottom).offset(margin);
        make.right.mas_equalTo(-margin);
        make.height.mas_equalTo(40);
    }];
    
    
    
    _contentLb.numberOfLines = 3;
    _contentLb.preferredMaxLayoutWidth = ScreenWidth - margin*2;
    [_contentLb sizeToFit];
    
    _headerImageview.layer.masksToBounds = YES;
    _headerImageview.layer.cornerRadius = 36/2;
    
    
    _beforImgView.layer.cornerRadius = 5;
    _beforImgView.layer.masksToBounds = YES;
    _afterImgView.layer.cornerRadius = 5;
    _afterImgView.layer.masksToBounds = YES;
    _beforImgView.contentMode = UIViewContentModeScaleAspectFill;
      _afterImgView.contentMode = UIViewContentModeScaleAspectFill;
    
    _beforImgView.userInteractionEnabled = YES;
    _beforImgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _afterImgView.userInteractionEnabled = YES;
    _afterImgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  
    
    
    _rewardView.backgroundColor = [UIColor cyanColor];

    self.hyb_lastViewInCell = _rewardView;
    self.hyb_bottomOffsetToCell = margin;
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    longPress.minimumPressDuration = 1; //定义按的时间
    longPress.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:longPress];
}

#pragma mark - 长按手势
- (void)longPressAction:(UILongPressGestureRecognizer*)longPress{
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按手势");
        if (self.shareBlock) {
            self.shareBlock();
        }
    }else{
        
    }
    
}

-(void)setModel:(WKContentListModel *)model{
    _model = model;
    [_headerImageview sd_setImageWithURL:[NSURL URLWithString:model.headerImagUrl] placeholderImage:nil];
    _nameLb.text = BLANKSTRING(model.name);
    _timeLb.text = @"1小时前";
    _contentLb.text = BLANKSTRING(model.content);
    
 
    _commentView.commentStr = BLANKSTRING(model.comment);
    
    [_beforImgView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545034633735&di=df96e5b9d61d6c221dbef27152ce4864&imgtype=0&src=http%3A%2F%2Fimage2.xyzs.com%2Fupload%2Fe0%2Fc6%2F421%2F20150506%2F143087296629259_0.jpg"]];
    [_afterImgView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545034633735&di=d72c584ea05ec7550c266d0c0964cc34&imgtype=0&src=http%3A%2F%2Fimg.chuansong.me%2Fmmbiz%2FeicAroOXE1MCSQyvVEhib2vds7fwbBvM8kNoTB9RiaBSB1LZEoTSnWhbFpVTW8wpZ3HRfyguW94cBOiciamSr9eqvrg%2F0%3Fwx_fmt%3Djpeg"]];
    
    
    CGFloat commentHeight = 0;
    if (model.comment.length>0) {
        commentHeight = [model.comment boundingRectWithSize:CGSizeMake(ScreenWidth-24-16, MAXFLOAT) font:PFSC_RegularFont(12) lineSpacing:5 maxLines:3];
        commentHeight += 30;
        [_commentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_beforImgView.mas_bottom).offset(12);
            make.height.mas_equalTo(commentHeight);
        }];
        
        
    }else{
        
        [_commentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_beforImgView.mas_bottom).offset(0);
            make.height.mas_equalTo(commentHeight);
        }];
        
    }
    
    
}

#pragma mark - 点击关注
- (void)guanzhuClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    _guanzhuBtn.backgroundColor = sender.selected == YES ? UIColorFromHex(0xF54050) : [UIColor whiteColor];
    //    _guanzhuBtn.layer.borderColor = sender.selected == YES ? UIColorFromHex(0xF54050).CGColor : [UIColor whiteColor].CGColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
